﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessObject.DAO
{
    public class PhanCongDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lophoc"></param>
        /// <returns></returns>
        public List<PHANCONG> getPhanCongByLopHoc(LOPHOC lophoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var query = from data in entities.PHANCONGs
                            where data.LopHocId == lophoc.LopHocId
                            select data;
                var _lophoc = entities.LOPHOCs.FirstOrDefault(p => p.LopHocId == lophoc.LopHocId);
                var monhocs = (from data in entities.CHITIETMONHOCs where data.KHOILOP.KhoiLopId == _lophoc.KhoiLopId select data).ToList();
                var list = query.ToList();
                if (list.Count < monhocs.Count)
                {
                    foreach (var obj in monhocs)
                    {
                        var res = list.FirstOrDefault(p => p.CHITIETMONHOC.ChiTietMonHocId == obj.ChiTietMonHocId);
                        if (res == null)
                        {
                            list.Add(new PHANCONG() { CHITIETMONHOC = obj, LOPHOC = _lophoc });
                        }
                    }
                }
                foreach (var obj in list)
                {
                    if (obj.CHITIETMONHOC.MONHOC != null)
                    {

                    }
                    if (obj.GIAOVIEN != null)
                    {

                    }
                }
                return list;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="phancong"></param>
        /// <param name="giaovien"></param>
        /// <returns></returns>
        public PHANCONG updatePhanCong(PHANCONG phancong, GIAOVIEN giaovien)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {

                var _phancong = entity.PHANCONGs.FirstOrDefault(p => p.ChiTietMonHocId == phancong.ChiTietMonHocId && p.LopHocId == phancong.LopHocId) ;
                var _giaovien = entity.USERs.OfType<GIAOVIEN>().FirstOrDefault(p => p.UserId == giaovien.UserId);
                if (_phancong == null)
                {
                    var _chitietmonhoc = entity.CHITIETMONHOCs.FirstOrDefault(p => p.ChiTietMonHocId == phancong.ChiTietMonHocId);
                    var _lophoc = entity.LOPHOCs.FirstOrDefault(p => p.LopHocId == phancong.LopHocId);
                    _phancong = new PHANCONG()
                    {
                        CHITIETMONHOC = _chitietmonhoc,
                        LOPHOC = _lophoc,
                        GIAOVIEN = _giaovien
                    };
                    entity.PHANCONGs.AddObject(_phancong);
                    entity.SaveChanges();
                }
                else
                {
                    _phancong.GIAOVIEN = _giaovien;
                    entity.SaveChanges();
                }
                
                if (_phancong.CHITIETMONHOC != null && _phancong.LOPHOC != null 
                    && _phancong.CHITIETMONHOC.KHOILOP != null && _phancong.CHITIETMONHOC.MONHOC != null) { }
               
                return _phancong;
            }
        }

        public static GIAOVIEN GetGiaoVien(PHANCONG phancong)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {

                var _phancong =  entity.PHANCONGs.FirstOrDefault(p => p.PhanCongId == phancong.PhanCongId);
                return _phancong.GIAOVIEN;
            }
        }
    }
}
